<template>
  <p-card :bordered="false" :class="'cust-erp-sub-tab'">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <p-form layout="inline" @keyup.enter.native="searchQuery">
        <p-row :gutter="24">
          <p-col :xl="6" :lg="7" :md="8" :sm="24">
            <p-form-item label="生产工单">
              <p-input placeholder="请输入生产工单" v-model="queryParam.productionno"></p-input>
            </p-form-item>
          </p-col>
           <p-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <p-button type="primary" @click="searchQuery" icon="search">查询</p-button>
              <!-- <p-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</p-button> -->
             
            </span>
          </p-col>
        </p-row>
      </p-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <!-- <p-button v-if="mainId" @click="handleAdd" type="primary" icon="plus">新增</p-button> -->

      <template v-if="cgButtonList && cgButtonList.length>0" v-for="(item,index) in cgButtonList">
        <p-button
          v-if=" item.optType=='js' "
          :key=" 'cgbtn'+index "
          @click="cgButtonJsHandler(item.buttonCode)"
          type="primary"
          :icon="item.buttonIcon"
        >{{ item.buttonName }}</p-button>
        <p-button
          v-else-if=" item.optType=='action' "
          :key=" 'cgbtn'+index "
          @click="cgButtonActionHandler(item.buttonCode)"
          type="primary"
          :icon="item.buttonIcon"
        >{{ item.buttonName }}</p-button>
      </template>

      
    </div>

    <!-- table区域-begin -->
    <div>
      

      <p-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :scroll="tableScroll"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        @change="handleTableChange"
      >
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无此图片</span>
          <img
            v-else
            :src="getImgView(text)"
            height="25px"
            alt="图片不存在"
            style="max-width:80px;font-size: 12px;font-style: italic;"
          />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无此文件</span>
          <p-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="uploadFile(text)"
          >下载</p-button>
        </template>

       
      </p-table>
    </div>

    <!-- <mesBcPrint-modal ref="modalForm" :EnhanceJS="EnhanceJS" @ok="modalFormOk" :mainId="mainId"></mesBcPrint-modal> -->
  </p-card>
</template>

<script>
import { JeecgListMixin } from "@/mixins/JeecgListMixin";
import MesBcPrintModal from "./modules/MesBcPrintModal";
import ButtonExpHandler from "@/components/jeecgbiz/modules/ButtonExpHandler";
import { postAction, getAction, deleteAction } from "@/api/manage";
import { filterObj } from "@/utils/util";

export default {
  name: "MesBcPrintList",
  mixins: [JeecgListMixin],
  components: { MesBcPrintModal },
  props: {
    mainId: {
      type: String,
      default: "",
      required: false,
    },
  },
  watch: {
    mainId: {
      handler(val) {
        if (!this.mainId) {
          this.clearList();
        } else {
          this.queryParam["productionno"] = val.productionno;
          this.loadData(1);
        }
      },
    },
  },
  data() {
    return {
      description: "工单标签报表管理页面",
      disableMixinCreated: true,
      EnhanceJS: "",
      cgButtonLinkList: [],
      cgButtonList: [],
      loading:false,
      code: "",
      // 表头
      columns: [
        {
          title: "序号",
          dataIndex: "",
          key: "rowIndex",
          width: 60,
          align: "center",
          customRender: function (t, r, index) {
            return parseInt(index) + 1;
          },
        },
        {
          title: "标签模板编号",
          align: "center",
          dataIndex: "labelTemplateNo",
        },
        {
          title: "标签模板名称",
          align: "center",
          dataIndex: "labelTemplateName",
        },
        {
          title: "工单号",
          align: "center",
          dataIndex: "productionno",
        },
        {
          title: "首标签条码",
          align: "center",
          dataIndex: "beginBarcode",
        },
        {
          title: "尾标签条码",
          align: "center",
          dataIndex: "endBarcode",
        },
        {
          title: "条码规则名称",
          align: "center",
          dataIndex: "ruleName",
        },
        {
          title: "数量",
          align: "center",
          dataIndex: "qty",
        },
        {
          title: "打印人",
          align: "center",
          dataIndex: "name",
        },
        {
          title: "打印时间",
          align: "center",
          dataIndex: "time",
           customRender: function (t) {
            let now = new Date(t);
            let year = now.getFullYear(); //取得4位数的年份
            let month = now.getMonth() + 1; //取得日期中的月份，其中0表示1月，11表示12月
            let date = now.getDate(); //返回日期月份中的天数（1到31）
            return year + "-" + month + "-" + date;
          },
        },
        
      ],
      url: {
        list: "/mes-code/mesBcPrint/list",
        delete: "/mes-code/auto/mesBcPrintOrder/deleteMesBcPrint",
        deleteBatch: "/mes-code/auto/mesBcPrintOrder/deleteBatchMesBcPrint",
        getColumns: "/mes-code/online/cgform/api/getColumns/",
        buttonAction: "/mes-code/online/cgform/api/doButton",
      },
      dictOptions: {
        alreadyPrint: [],
      },
    };
  },

  computed: {
    tableScroll: function () {
      if (this.columns.length > 12) {
        return { x: this.columns.length * 160 + 40 };
      }
    },
  },

  created() {
    this.setdefColumns();
  },
  methods: {
    setdefColumns() {
      if (this.columns.length > 12) {
        let arrLength = this.columns.length - 1;
        this.columns[0]["width"] = 70;
        this.columns[0]["fixed"] = "left";
      }
    },

    clearList() {
      this.dataSource = [];
      this.selectedRowKeys = [];
      this.ipagination.current = 1;
    },
    //2020/7/9
    initDictConfig() {},
    showLinkButton(item, record) {
      let btn = new ButtonExpHandler(item.exp, record);
      return btn.show;
    },
    
    initCgEnhanceJs(enhanceJs) {
      //console.log("--onlineList-js增强",enhanceJs)
      if (enhanceJs) {
        let Obj = eval("(" + enhanceJs + ")");
        this.EnhanceJS = new Obj(getAction, postAction, deleteAction);
        this.cgButtonJsHandler("created");
      } else {
        this.EnhanceJS = "";
      }
    },
    initCgButtonList(btnList) {
      let linkArr = [];
      let buttonArr = [];
      if (btnList && btnList.length > 0) {
        for (let i = 0; i < btnList.length; i++) {
          let temp = btnList[i];
          if (temp.buttonStyle == "button") {
            buttonArr.push(temp);
          } else if (temp.buttonStyle == "link") {
            linkArr.push(temp);
          }
        }
      }
      this.cgButtonLinkList = [...linkArr];
      this.cgButtonList = [...buttonArr];
      console.log("链接增强", this.cgButtonLinkList);
    },
    cgButtonJsHandler(buttonCode) {
      if (this.EnhanceJS[buttonCode]) {
        this.EnhanceJS[buttonCode](this);
      }
    },
    cgButtonActionHandler(buttonCode) {
      //处理自定义button的 需要配置该button自定义sql
      if (!this.selectedRowKeys || this.selectedRowKeys.length == 0) {
        this.$message.warning("请先选中一条记录");
        return false;
      }
      if (this.selectedRowKeys.length > 1) {
        this.$message.warning("请只选中一条记录");
        return false;
      }
      let params = {
        formId: this.code,
        buttonCode: buttonCode,
        dataId: this.selectedRowKeys[0],
      };
      console.log("自定义按钮请求后台参数：", params);
      postAction(this.url.buttonAction, params).then((res) => {
        if (res.success) {
          this.loadData();
          this.$message.success("处理完成!");
        } else {
          this.$message.warning("处理失败!");
        }
      });
    },
    cgButtonLinkHandler(record, buttonCode, optType) {
      if (optType == "js") {
        if (this.EnhanceJS[buttonCode]) {
          this.EnhanceJS[buttonCode](this, record);
        }
      } else if (optType == "action") {
        let params = {
          formId: this.code,
          buttonCode: buttonCode,
          dataId: record.id,
        };
        console.log("自定义按钮link请求后台参数：", params);
        postAction(this.url.buttonAction, params).then((res) => {
          if (res.success) {
            this.loadData();
            this.$message.success("处理完成!");
          } else {
            this.$message.warning("处理失败!");
          }
        });
      }
    },
    loadData(arg) {
      if (!this.url.list) {
        this.$message.error("请设置url.list属性!");
        return;
      }
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1;
      }
      this.onClearSelected();
      this.loading = true;
     
        var newparams={
          limit:this.ipagination.pageSize,
          pageNo:this.ipagination.current,
          productionno:this.queryParam.productionno
        }
      getAction(this.url.list, newparams).then((res) => {
        if (res.success) {
          this.dataSource = res.data.records;
          this.ipagination.total = res.data.total;
        }
        if (res.code === 510) {
          this.$message.warning(res.msg);
        }
        this.loading = false;
      });
    },
  },
};
</script>
<style scoped>
@import "~@assets/less/common.less";
</style>
